package cn.iocoder.yudao.module.biz.controller.admin.receivables;

import cn.iocoder.yudao.framework.excel.core.util.poi.ExcelUtilRuoyi;
import cn.iocoder.yudao.module.biz.controller.admin.invoice.domain.SanBizInvoiceManagement;
import cn.iocoder.yudao.module.biz.controller.admin.pay.domain.SanBizPaymentManagement;
import cn.iocoder.yudao.module.biz.controller.admin.pay.service.SanBizPaymentManagementService;
import cn.iocoder.yudao.module.biz.controller.admin.receivables.domain.SanBizContractArchiveReceiveRecord;
import cn.iocoder.yudao.module.biz.controller.admin.receivables.service.SanBizContractArchiveReceiveRecordService;
import cn.iocoder.yudao.module.biz.service.contract.ContractArchiveService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 应收款管理")
@RestController
@RequestMapping("/biz/contract-archive-receive-record")
@Validated
public class BizContractArchiveReceiveRecordController {

    @Resource
    private SanBizContractArchiveReceiveRecordService contractArchiveReceiveRecordService;
    @Resource
    private ContractArchiveService contractArchiveService;

    @Resource
    private SanBizPaymentManagementService paymentManagementService;
//    @PostMapping("/create")
////    @Operation(summary = "创建应收款管理")
//    @PreAuthorize("@ss.hasPermission('biz:contract-archive-receive-record:create')")
//    public CommonResult<Long> createContractArchiveReceiveRecord(@Valid @RequestBody SanBizContractArchiveReceiveRecord createReqVO) {
//        contractArchiveReceiveRecordService.save(createReqVO);
//        return success(1l);
//    }

    @PutMapping("/update")
    @Operation(summary = "更新应收款管理")
    @PreAuthorize("@ss.hasPermission('biz:contract-archive-receive-record:update')")
    public CommonResult<Boolean> updateContractArchiveReceiveRecord(@Valid @RequestBody SanBizContractArchiveReceiveRecord updateReqVO) {
        // 判断是否收款
        if(!"3".equals(updateReqVO.getStatus())) {
            QueryWrapper<SanBizPaymentManagement> wrapper = new QueryWrapper<>();
            wrapper.eq("contract_receive_record_id",updateReqVO.getId());
            wrapper.eq("deleted", 0);
            List<SanBizPaymentManagement> san = paymentManagementService.list(wrapper);
            if(san != null && san.size() > 0){
                updateReqVO.setStatus("1");
            } else {
                updateReqVO.setStatus("0");
            }
        }
        contractArchiveReceiveRecordService.updateById(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
////    @Operation(summary = "删除应收款管理")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:contract-archive-receive-record:delete')")
//    public CommonResult<Boolean> deleteContractArchiveReceiveRecord(@RequestParam("id") Long id) {
//        contractArchiveReceiveRecordService.removeById(id);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete-list")
//    @Parameter(name = "ids", description = "编号", required = true)
////    @Operation(summary = "批量删除应收款管理")
//                @PreAuthorize("@ss.hasPermission('biz:contract-archive-receive-record:delete')")
//    public CommonResult<Boolean> deleteContractArchiveReceiveRecordList(@RequestParam("ids") List<Long> ids) {
//        contractArchiveReceiveRecordService.removeByIds(ids);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得应收款管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:contract-archive-receive-record:query')")
    public CommonResult<SanBizContractArchiveReceiveRecord> getContractArchiveReceiveRecord(@RequestParam("id") Long id) {
        SanBizContractArchiveReceiveRecord contractArchiveReceiveRecord = contractArchiveReceiveRecordService.getById(id);
        contractArchiveReceiveRecord.setContractArchiveInfo(contractArchiveService.getContractArchive(contractArchiveReceiveRecord.getContractArchiveId()));
        return success(BeanUtils.toBean(contractArchiveReceiveRecord, SanBizContractArchiveReceiveRecord.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得应收款管理分页")
    @PreAuthorize("@ss.hasPermission('biz:contract-archive-receive-record:query')")
    public CommonResult<IPage<SanBizContractArchiveReceiveRecord>> getContractArchiveReceiveRecordPage(@Valid SanBizContractArchiveReceiveRecord pageReqVO) {
//        PageResult<BizContractArchiveReceiveRecordDO> pageResult = contractArchiveReceiveRecordService.getContractArchiveReceiveRecordPage(pageReqVO);
        IPage<SanBizContractArchiveReceiveRecord> pageResult = contractArchiveReceiveRecordService.getContractArchiveReceiveRecordPage2(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出应收款管理 Excel")
    @PreAuthorize("@ss.hasPermission('biz:contract-archive-receive-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractArchiveReceiveRecordExcel(@Valid SanBizContractArchiveReceiveRecord pageReqVO,
              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<BizContractArchiveReceiveRecordDO> list = contractArchiveReceiveRecordService.getContractArchiveReceiveRecordPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "应收款管理.xls", "数据", SanBizContractArchiveReceiveRecord.class,
//                        BeanUtils.toBean(list, SanBizContractArchiveReceiveRecord.class));
        QueryWrapper w = new QueryWrapper(pageReqVO);
        List<SanBizInvoiceManagement> list = contractArchiveReceiveRecordService.list(w);
        // 导出 Excel
//        ExcelUtils.write(response, "打款记录.xls", "数据", SanBizPaymentManagement.class,
//                        BeanUtils.toBean(list, SanBizPaymentManagement.class));
        ExcelUtilRuoyi<SanBizInvoiceManagement> util = new ExcelUtilRuoyi<SanBizInvoiceManagement>(SanBizInvoiceManagement.class);
        util.exportExcel(response, list, "应收款列表");
    }

    @GetMapping("/getContractArchiveReceiveRecordList")
    @Operation(summary = "获得应收款管理列表")
    @PreAuthorize("@ss.hasPermission('biz:contract-archive-receive-record:query')")
    public CommonResult<List<SanBizContractArchiveReceiveRecord>> getContractArchiveReceiveRecordList(@Valid SanBizContractArchiveReceiveRecord pageReqVO) {
        List<SanBizContractArchiveReceiveRecord> pageResult = contractArchiveReceiveRecordService.getContractArchiveReceiveRecordList(pageReqVO);
        return success(pageResult);
    }

}
